package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.SolicitudLocal;
import co.gov.medellin.sdep.sisdep.model.dao.FeriaDecFormVentaDAOImpl.ListaDistribucionQueryOperation;

public class SolicitudLocalDAOImpl extends JdbcDaoSupport implements ISolicitudLocalDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindByRadicadoQuery findByRadicadoQuery;
    
    private ListaCentrosComerciales listaCco;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM CCO_SOLICITUD_LOCAL_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(SolicitudLocal solicitudLocal) {
		deleteOperation.execute( solicitudLocal  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public SolicitudLocal findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(SolicitudLocal solicitudLocal) {
		insertOperation.execute( solicitudLocal );
	}

	public void update(SolicitudLocal solicitudLocal) {
		updateOperation.execute( solicitudLocal );
	}

	public SolicitudLocal findByCriteria(SolicitudLocal  solicitudLocal) {
		return findByCriteriaQuery.runQuery(solicitudLocal);
	}

	public List load() {
		return findAllQuery.runQuery();
	}

	public List loadByRadicado(SolicitudLocal solicitudLocal) {
		return findByRadicadoQuery.runQuery(solicitudLocal);
	}
	
	public List listaCentrosComerciales() {
		return listaCco.runQuery();
	}

	public SolicitudLocal findByActiveState(SolicitudLocal  solicitudLocal) {
		return findByActiveStateQuery.runQuery(solicitudLocal);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByRadicadoQuery = new FindByRadicadoQuery( getDataSource() );
    	listaCco = new ListaCentrosComerciales(getDataSource());
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO CCO_SOLICITUD_LOCAL_T ("
				  + " ID, "
				  + " ID_SOLICITUD, "
				  + " ID_CENTRO_COMERCIAL, "
				  + " ID_TIPOLOGIA, "
				  + " DESCRIPCION, "
				  + " OBSERVACIONES, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "				  
				  + ") VALUES ("
			  	  + " CCO_SOLICITUD_LOCAL_SEC.nextVal, "
				  + "(SELECT ID FROM ADM_SOLICITUD_T WHERE RADICADO=?),"
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(SolicitudLocal solicitudLocal) {
			InsertOperation.this.update( new Object[]{				
				solicitudLocal.getRadicado(),
				solicitudLocal.getIdCentroComercial(),
				solicitudLocal.getIdTipologia(),
				solicitudLocal.getDescripcion(),
				solicitudLocal.getObservaciones(),
				solicitudLocal.getEstado(),
				solicitudLocal.getUsuarioCrea()				
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE CCO_SOLICITUD_LOCAL_T SET "					  
					  + " ID_SOLICITUD = (SELECT ID FROM ADM_SOLICITUD_T WHERE RADICADO=?),"
					  + " ID_CENTRO_COMERCIAL = ? ,"
					  + " ID_TIPOLOGIA = ? ,"
					  + " DESCRIPCION = ? ,"
					  + " OBSERVACIONES = ? ,"
					  + " ESTADO = ? ,"					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE"
				  	  + WHERE
				  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(SolicitudLocal solicitudLocal) {
			UpdateOperation.this.update( new Object[]{				
				solicitudLocal.getRadicado(),
				solicitudLocal.getIdCentroComercial(),
				solicitudLocal.getIdTipologia(),
				solicitudLocal.getDescripcion(),
				solicitudLocal.getObservaciones(),
				solicitudLocal.getEstado(),				
				solicitudLocal.getUsuarioModifica(),				
				solicitudLocal.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM CCO_SOLICITUD_LOCAL_T "
										+ WHERE + "1=1";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(SolicitudLocal solicitudLocal) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM CCO_SOLICITUD_LOCAL_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			SolicitudLocal solicitudLocal = new SolicitudLocal();
				solicitudLocal.setId(new Long(rs.getLong("ID")));
				solicitudLocal.setIdSolicitud(new Long(rs.getLong("ID_SOLICITUD")));
				solicitudLocal.setIdCentroComercial(new Long(rs.getLong("ID_CENTRO_COMERCIAL")));
				solicitudLocal.setIdCentroComercialStr(rs.getString("ID_CENTRO_COMERCIAL"));
				solicitudLocal.setIdTipologia(new Long(rs.getLong("ID_TIPOLOGIA")));
				solicitudLocal.setIdTipologiaStr(rs.getString("ID_TIPOLOGIA"));
				solicitudLocal.setDescripcion(rs.getString("DESCRIPCION"));
				solicitudLocal.setObservaciones(rs.getString("OBSERVACIONES"));
				solicitudLocal.setEstado(rs.getString("ESTADO"));
				solicitudLocal.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				solicitudLocal.setFechaCrea(rs.getDate("FECHA_CREA"));		
				solicitudLocal.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				solicitudLocal.setFechaModifica(rs.getDate("FECHA_MODIFICA"));
				solicitudLocal.setStrCentroComercial(rs.getString("CCO_NOMBRE"));
				solicitudLocal.setStrTipologia(rs.getString("TIPOLOGIA"));				
				solicitudLocal.setRadicado(rs.getString("RADICADO"));
			return solicitudLocal;
		}
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class ListaCentroComercialQueryOperation extends MappingSqlQuery {
		public ListaCentroComercialQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
				ArrayList typeValueList = new ArrayList();
				String registro[] = {"0","0"};
				registro[0]= rs.getString("ID");
				registro[1]= rs.getString("NOMBRE");
				typeValueList.add(registro);
				registro = null;
			return typeValueList;
		}
    }
	
    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public SolicitudLocal runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (SolicitudLocal) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_SOLICITUD = ? "
			  	  	  					//+ " AND ID_CENTRO_COMERCIAL = ? "
			  	  	  					//+ " AND ID_TIPOLOGIA = ? "
			  	  	  					//+ " AND DESCRIPCION = ? "
			  	  	  					//+ " AND OBSERVACIONES = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public SolicitudLocal runQuery(SolicitudLocal solicitudLocal) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//solicitudLocal.getId(),
				//solicitudLocal.getIdSolicitud(),
				//solicitudLocal.getIdCentroComercial(),
				//solicitudLocal.getIdTipologia(),
				//solicitudLocal.getDescripcion(),
				//solicitudLocal.getObservaciones(),
				//solicitudLocal.getEstado(),
				//solicitudLocal.getUsuarioCrea(),
				//solicitudLocal.getFechaCrea(),
				//solicitudLocal.getUsuarioModifica(),
				//solicitudLocal.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (SolicitudLocal) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }
    
    // ------------- FindByRadicadoQuery ----------------------------------------    
    class FindByRadicadoQuery extends BaseQueryOperation {
		private static final String sql = "SELECT SEL.*, CCO.NOMBRE AS CCO_NOMBRE, MAE.VRT_NOMBRE AS TIPOLOGIA,RADICADO "
										+ " FROM CCO_SOLICITUD_LOCAL_T SEL INNER JOIN "
										+ " CCO_CENTRO_COMERCIAL_T CCO ON SEL.ID_CENTRO_COMERCIAL=CCO.ID INNER JOIN "
										+ " ADM_VALOR_TIPO_MAESTRA_T MAE ON SEL.ID_TIPOLOGIA = MAE.ID_VALOR_TIPO INNER JOIN "
										+ " ADM_SOLICITUD_T SOLI ON SOLI.ID = SEL.ID_SOLICITUD "
										+ " WHERE RADICADO = ?";

    	public FindByRadicadoQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(SolicitudLocal solicitudLocal) {
			List result =  execute( new Object[]{
							solicitudLocal.getRadicado()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public SolicitudLocal runQuery(SolicitudLocal solicitudLocal) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (SolicitudLocal) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    class ListaCentrosComerciales extends ListaCentroComercialQueryOperation{
		private static final String sql = "SELECT ID,NOMBRE FROM CCO_CENTRO_COMERCIAL_T";
		public ListaCentrosComerciales(DataSource ds) {
					super(ds,sql);
					compile();
		}
		public List runQuery() {
				List result =  execute();
				if ( result.size() > 0 ) {
					return result;
				} else {
					return null;
				}
		}
}    
    
}
